package com.ming.luckyDemo.service.serviceImpl;

import com.ming.luckyDemo.mapper.BetBillMapper;
import com.ming.luckyDemo.pojo.DO.BetBillDO;
import com.ming.luckyDemo.pojo.DO.UserInfoDO;
import com.ming.luckyDemo.pojo.entity.ResultBody;
import com.ming.luckyDemo.service.BetBillService;
import com.ming.luckyDemo.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Tcm
 */
@Service
public class BetBillServiceImpl implements BetBillService {
    @Autowired
    private BetBillMapper betBillMapper;

    @Override
    public ResultBody saveBet(BetBillDO betBillDO) {
        if ("".equals(betBillDO.getSite())||
        "".equals(betBillDO.getType())){
            return ResultBody.error().message("信息缺失，投注失败");
        }
        DateUtil dateUtil = new DateUtil();
        String currentTime = dateUtil.getCurrentTime();
        String substring = currentTime.substring(currentTime.length() - 2);
        int num = Integer.parseInt(substring);
        if (num<=55){
            int i = betBillMapper.saveBet(betBillDO.getNumbers(), betBillDO.getSite(), betBillDO.getType(), betBillDO.getBet(), currentTime, betBillDO.getUsername());
            if (i>0){
                return ResultBody.ok().message("下注成功");
            }
            return ResultBody.error().message("下注失败");
        }
        return ResultBody.error().message("本次以封盘");
    }

    @Override
    public UserInfoDO getCookie(String cookieName) {
        return betBillMapper.queryByName(cookieName);
    }

    @Override
    public boolean selectBalance(BetBillDO betBillDO) {
        double v = betBillMapper.selectBalance(betBillDO.getUsername());
        boolean a = v<betBillDO.getBet()?false:true;
        if (a){
            double newBanlance = v-betBillDO.getBet();
            betBillMapper.updateBanlance(newBanlance,betBillDO.getUsername());
        }
        return a;
    }
}
